package cn.fly.identification.utils;

import com.baidu.aip.face.AipFace;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;



public class IdentificationUtils {

    public static final Logger log = LoggerFactory.getLogger(IdentificationUtils.class);

    public static void GenerateImage(String faceBase, File file) {
        try {
            // 解码base64字符串为字节数组
            byte[] imageBytes = Base64.getDecoder().decode(faceBase);

            // 创建文件输出流
            FileOutputStream fos = new FileOutputStream(file);

            // 将字节数组写入文件
            fos.write(imageBytes);

            // 关闭文件输出流
            fos.close();

            System.out.println("图像保存成功：" + file.getAbsolutePath());
        } catch (IOException e) {
            System.out.println("图像保存失败：" + e.getMessage());
        }
    }

    public static String register(String username, String faceBase, String appID, String apiKey, String secretKey) {
        if (StringUtils.hasLength(username) && StringUtils.hasLength(faceBase)) {
            String upPath = "D:\\photo";
            upPath = upPath.replace("/" , "\\");
            log.info("upPath = {}", upPath);

            String fileName = username + System.currentTimeMillis() + ".png";
            System.out.println(upPath + "\\" + fileName);
            File file = new File(upPath + "\\" + fileName);

            //调用百度云服务
            AipFace client = new AipFace(appID, apiKey, secretKey);
            //本地存储
            GenerateImage(faceBase, file);
            //上传到百度云人脸库
            facesetAddUser(client, faceBase, username);

            return upPath + "\\" + fileName;
        }

        return "录入失败";
    }

    public static void facesetAddUser(AipFace client, String faceBase, String username) {
        // 设置可选参数
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("user_info", "user info");

        // 调用人脸库中添加用户接口
        JSONObject response = client.addUser(faceBase, "BASE64", "frankiefly12138", username, options);

        // 打印返回的结果
        System.out.println(response.toString(2));
    }

    /**
     * 人脸比对
     * @param imgBash64 照片转bash64格式
     * @return
     */
    public static Double verifyUser(String imgBash64,AipFace client) {
        // 传入可选参数调用接口
        HashMap<String, String> options = new HashMap<String, String>();

        JSONObject res = client.search(imgBash64, "BASE64", "frankiefly12138", options);

        System.out.println(res.toString(2));
        System.out.println(res.getJSONObject("result"));
        System.out.println(res.getJSONObject("result").getJSONArray("user_list"));
        JSONObject user = (JSONObject) res.getJSONObject("result").getJSONArray("user_list").get(0);

        return user.getDouble("score");
    }

    public static Double login(String faceBase,  String appID, String apiKey, String secretKey) {
        AipFace client = new AipFace(appID, apiKey, secretKey);
        Double num = verifyUser(faceBase, client);
        return num;
    }
}
